package com.ruoyi.web.controller.authentication;


import cn.hxzy.common.CertificationStatus;
import cn.hxzy.common.GenderStatus;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxzy.authentication.domain.TsRealNameAuthentication;
import com.hxzy.authentication.service.TsRealNameAuthenticationService;
import com.hxzy.user.service.TmUserService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.DataSourceType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mingqian.Wu
 * @since 2023-06-23
 */
@RestController
@RequestMapping("/tsRealNameAuthentication")
@DataSource(DataSourceType.AUTHENTICATION)
public class TsRealNameAuthenticationController extends BaseController {

    @Autowired
    private TsRealNameAuthenticationService tsRealNameAuthenticationService;

    @Autowired
    private TmUserService tmUserService;

    @Value("${oss-img-path}")
    private String ossImgPath;

    @GetMapping("getCertificationList/{pageIndex}/{pageSize}")
    @Anonymous
    public AjaxResult getCertificationList(@PathVariable Integer pageIndex, @PathVariable Integer pageSize) throws SQLException {
        PageHelper.startPage(pageIndex, pageSize);
        QueryWrapper<TsRealNameAuthentication>queryWrapper=new QueryWrapper<>();
        queryWrapper.notInSql("certification_status","101");
        List<TsRealNameAuthentication> list = tsRealNameAuthenticationService.list(queryWrapper);
        list.stream().forEach(item -> {
            item.setCertificationName(CertificationStatus.getCertificationStatusValue(item.getCertificationStatus()));
        });
        PageInfo pageInfo = new PageInfo(list);
        long total = pageInfo.getTotal();
        return AjaxResult.success(pageInfo.getList()).put("total", total);
    }

    @PostMapping("/searchCertification/{pageIndex}/{pageSize}")
    public AjaxResult searchCertification(@RequestBody TsRealNameAuthentication tsRealNameAuthentication, @PathVariable Integer pageIndex, @PathVariable Integer pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        List<TsRealNameAuthentication> list = tsRealNameAuthenticationService.searchCertification(tsRealNameAuthentication);
        PageInfo pageInfo = new PageInfo(list);
        long total = pageInfo.getTotal();
        list.stream().forEach(item -> {
            item.setCertificationName(CertificationStatus.getCertificationStatusValue(item.getCertificationStatus()));
        });
        return AjaxResult.success(pageInfo.getList()).put("total", total);
    }

    @GetMapping("/getCertificationListByStatus/{pageIndex}/{pageSize}/{status}")
    public AjaxResult getCertificationListByStatus(@PathVariable Integer pageIndex, @PathVariable Integer pageSize, @PathVariable Integer status) {
        PageHelper.startPage(pageIndex, pageSize);
        QueryWrapper<TsRealNameAuthentication> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("certification_status", status);
        List<TsRealNameAuthentication> list = tsRealNameAuthenticationService.list(queryWrapper);
        PageInfo pageInfo = new PageInfo(list);
        long total = pageInfo.getTotal();
        list.stream().forEach(item -> {
            item.setCertificationName(CertificationStatus.getCertificationStatusValue(item.getCertificationStatus()));
        });
        return AjaxResult.success(pageInfo.getList()).put("total", total);
    }

    @GetMapping("/getCertificationInfo/{id}")
    public AjaxResult getCertificationInfo(@PathVariable String id) {
        TsRealNameAuthentication byId = tsRealNameAuthenticationService.getById(id);
        byId.setIdCardFront(ossImgPath + byId.getIdCardFront());
        byId.setIdCardOpposite(ossImgPath + byId.getIdCardOpposite());
        byId.setGender(GenderStatus.getGenderStatusValue(Integer.parseInt(byId.getGender())));
        return AjaxResult.success(byId);
    }

    @PutMapping("/approved/{userId}")
    public AjaxResult approved(@RequestBody TsRealNameAuthentication tsRealNameAuthentication,@PathVariable String userId) {

        UpdateWrapper<TsRealNameAuthentication> tsRealNameAuthenticationUpdateWrapper = new UpdateWrapper<>();
        System.out.println(tsRealNameAuthentication.getAuthenticationId());
        tsRealNameAuthenticationUpdateWrapper.eq("authentication_id",tsRealNameAuthentication.getAuthenticationId());
        tsRealNameAuthenticationUpdateWrapper.set("certification_status",103);
        boolean update = tsRealNameAuthenticationService.update(tsRealNameAuthenticationUpdateWrapper);
        boolean b = tmUserService.updateStatus(userId,101);

        if (!update) {
            return AjaxResult.error("操作失败");
        }

        if (!b){
            return AjaxResult.error();
        }

        return AjaxResult.success("操作通过");
    }

    @PostMapping("/noApproved/{userId}")
    public AjaxResult noApproved(@RequestParam String id,@RequestParam String  rejection,@PathVariable String userId){
        TsRealNameAuthentication tsRealNameAuthentication=new TsRealNameAuthentication();
        tsRealNameAuthentication.setRejectReason(rejection);
        tsRealNameAuthentication.setAuthenticationId(id);
        tsRealNameAuthentication.setCertificationStatus(104);
        UpdateWrapper<TsRealNameAuthentication>updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("authentication_id",tsRealNameAuthentication.getAuthenticationId());
        boolean update = tsRealNameAuthenticationService.update(tsRealNameAuthentication, updateWrapper);
        boolean b = tmUserService.updateStatus(userId,103);
        if (!b){
            return AjaxResult.error();
        }

        if (!update){
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }


}

